$(function() {
    init();
});

/* ＊＊＊＊＊＊＊＊＊＊＊      creatHtml  页面生成     ＊＊＊＊＊＊＊＊ */

function init(){
    for (var i = 0; i < questionJson.length; i++) {
        var type = questionJson[i].type;
        switch (type) {
            case 'radio':
                radioHtml(questionJson[i], i,type);
                break;
            case 'checkbox':checkboxHtml(questionJson[i], i,type);
                break;
            case 'input':
                inputHtml(questionJson[i],type);
                break;
        }
    };
}
var creatHtml;
var radioHtml = function(data, index,type) {
    q_content(data,type);
    a_list(data, index,type);
};

var inputHtml = function(data,type) {
    var creatHtml = '<div class="question input"><h4><span>' + data.question + '</span><input type="text" onblur="inputBlur(this)" value="'+(data.value!=undefined?data.value:"")+'" style="text-indent:10px"></h4></div>';
    $("#question_list").append(creatHtml);
};
var checkboxHtml = function(data, index,type) {
    q_content(data,type);
    a_list(data, index,type);
};

//题目
function  q_content(data,type){
    creatHtml = '<div class="question '+type+'" >' +
        '<h4>' + data.question + '</h4>' +
        '<ul class="list" >';
}
//答案选项
function a_list(data, index,type){
    var className="ld_"+type;
    if (data.answer.answerArray.length > 0) {
        for (var i = 0; i < data.answer.answerArray.length; i++) {
            if(data.answer.answerArray[i].disease){
                    creatHtml += '<li class="'+className+'">' +
                    '<input type="'+type+'"  name="'+type+index + '">' +
                    '<span data-index='+i+'>' + data.answer.answerArray[i].disease + '</span>' +
                    '</li>';
            }else{
                creatHtml += '<li class="'+className+' '+checkedActive(data.answer.checked,data.answer.answerArray[i],type)+'">' +
                '<input type="'+type+'" '+checked(data.answer.checked,data.answer.answerArray[i],type)+' name="'+type+index + '">' +
                '<span data-index='+i+'>' + data.answer.answerArray[i] + '</span>' +
                '</li>';
            }
        };
        creatHtml += '</ul>';
        ifHide(data,index);
    }
    creatHtml += '</div>';
    $("#question_list").append(creatHtml);
}
function checkedActive(checked,answer,type){
    if(checked!=undefined&&checked!=""){
        if(checked==answer){
            switch (type){
                case "radio":return "active";break;
                default: return"";break;
            }
        }
        return""
    }
    return "";
}
function  checked(checked,answer,type){

    if(checked!=undefined&&checked!=""){
        if(checked==answer){
            switch (type){
                case "radio":return "checked=\"checked\"";break;
                default: return"";break;
            }
        }
        return""
    }
    return "";
}

//隐藏题目
function ifHide(data,index){
    if(data.answer.hide){
        var hideType=data.answer.hide[0].type;
        switch(hideType){
            case 'radio':
                creatHide(data,index,hideType);
            break;
            case 'checkbox':
                creatHide(data,index,hideType);
            break;
            case 'input':
                // creatHtml += '<div class="hide input" ><h4 style="overflow:hidden;"><label style="float:left;">' +data.answer.hide[0].question + '</label><input type="text" style="float:left;text-indent:10px;border:none;border-bottom:1px solid #d7d7d7;"></h4></div>';
                creatHtml+='<div class="hide input"><h4><span>' + data.answer.hide[0].question + '</span><input type="text" onblur="inputBlur(this)" style="text-indent:10px"></h4></div>'
            break;

        }
    }
}

function creatHide(data,index,hideType){
    creatHtml+='<div class="hide checkbox" style="display:none;">';
    creatHtml+='<h4>' + data.answer.hide[0].question + '</h4>' +
    '<ul class="list">';
    for (var i = 0; i < data.answer.hide[0].answer.length; i++) {
        var className="ld_"+hideType;
        creatHtml += '<li class="'+className+'">' +
            '<input type="'+hideType+'"  name="hide_'+hideType+index + '">' +
            '<span data-index="'+i+'">' + data.answer.hide[0].answer[i] + '</span>' +
            '</li>';
    };
    creatHtml+='</ul>';
}

/* ＊＊＊＊＊＊＊＊＊＊＊      creatHtml  页面生成     ＊＊＊＊＊＊＊＊ */

function onSelecting(obj){
    if(obj.hasClass("ld_radio")){
        obj.siblings('li').removeClass('active').find('input').removeAttr('checked');
        obj.addClass('active').find("input").attr("checked",true);
    }else if(obj.hasClass('ld_checkbox')){
        if(obj.hasClass('disabled')){
            return;
        }else if(obj.hasClass("active")){
            obj.removeClass('active').find('input').removeAttr('checked');
        }else{
            obj.addClass('active').find("input").attr("checked",true);
        }
    }
    //
    obj.parent().parent().attr('data-status','do');
}

var answerArray=[];
function getAnswer(index,obj){
    $this=$(obj)
    var answerTxt=$this.find('span').text();
    var answerNo=$this.index();
    var data={
        txt:answerTxt,
        No:answerNo
    };
    answerArray[index]=data;
}

function round(v,e){
    var t=1;
    for(;e>0;t*=10,e--);
    for(;e<0;t/=10,e++);
    return Math.round(v*t)/t;
}

    var dataArr=[];
    function  getData(){
        $(".question").each(function(index){

            var t=$(this);
            var radio=t.hasClass('radio');
            var checkbox=t.hasClass("checkbox");
            var input=t.hasClass('input');
            if(radio){
                getRadio_data(t,index);
            }else if(checkbox){
                getCheckbox_data(t,index);
            }else if(input){
                getInput_data(t,index);
            }
        })
        //alert(JSON.stringify(dataArr));
    }

    function getRadio_data(t,index){
       var question=$(t).find("h4").html();
       var question_no=index;
       var select_a=$(t).find("input:checked").next("span");
       var answer=select_a.html();
       var answer_no=select_a.attr("data-index");
       var answers=[];
       var answers_ele={
            answer_no:answer_no,
            answer:answer,
        };
        answers.push(answers_ele);

        var data;

        if($(t).find(".hide")){
            data={
                question_no:question_no,
                question:question,
                answer:answers,
                hide:getHide(t,index)
            };
        }else{
            data={
                question_no:question_no,
                question:question,
                answer:answers,
              };
        }
      
      dataArr[index]=data;
    }

    function getInput_data(t,index){
        var obj=$(t).find("h4 span");
        var question=obj.html();
        var question_no=index;
        var answer_no=0;
        var answer=$(t).find("h4 input").val();
        var answers=[];
        var answers_ele={
            answer_no:answer_no,
            answer:answer,
        }
        answers.push(answers_ele);
        var data={
            question_no:question_no,
            question:question,
            answer:answers,
        }; 
        dataArr[index]=data;
    }
    function  getCheckbox_data(t,index){
        var question=$(t).find("h4").html();
        var question_no=index;
        var checked=$(t).find("input:checked");
        var answerArr=[];
        for(var i=0;i<checked.length;i++){
            var select_a=checked.eq(i).siblings('span');
            var answer=select_a.html();
            var answer_no=select_a.attr("data-index");
            var data={
                answer_no:answer_no,
                answer:answer,
            };
            answerArr[i]=data;
        }
        var data;

        if($(t).find(".hide")){
            data={
                question_no:question_no,
                question:question,
                answer:answerArr,
                hide:getHide(t,index)
            };
        }else{
            data={
                question_no:question_no,
                question:question,
                answer:answerArr,
              };
        }
        dataArr[index]=data;
    }

    function getHide(t,index){
        var t=$(t).find(".hide");
        var radio=t.hasClass('radio');
        var checkbox=t.hasClass("checkbox");
        var input=t.hasClass('input');
        var hideData;
        if(radio){
           hideData=getHide_radio(t,index);
        }else if(checkbox){
            hideData=getHide_checkbox(t,index);
        }else if(input){
            hideData=getHide_input(t,index);
        }
        return hideData;
    }

    function getHide_checkbox(t,index){
        var question=$(t).find("h4").html();
        var quesetion_no=0;
        var checked=$(t).find("input:checked");
        var answerArr=[];
        for(var i=0;i<checked.length;i++){
            var select_a=checked.eq(i).siblings("span");
            var answer=select_a.html();
            var answer_no=select_a.attr("data-index");
            var data={
                answer_no:answer_no,
                answer:answer,
            };
            answerArr[i]=data;
        };
        var data={
            question_pid:index,
            question_no:0,
            question:question,
            answer:answerArr,
        };
        return data;
    }

    function getHide_radio(t,index){
        var question=$(t).find("h4").html();
        var select_a=$(t).find("input:checked").next("span");
        var answer=select_a.html();
        var answer_no=select_a.attr("data-index");
        var answers=[];
        var answers_ele={
            answer_no:answer_no,
            answer:answer,
        }
        answers.push(answers_ele);
        var data={
          question_pid:index,
          question_no:0,
          question:question,
          answer:answers,
        };
        return data;
    }

    function getHide_input(t,index){
        var obj=$(t).find("h4 span");
        var question=obj.html();
        var question_no=index;
        var answer_no=0;
        var answer=$(t).find("h4 input").val();
        var answers=[];
        var answers_ele={
            answer_no:answer_no,
            answer:answer,
        }
        answers.push(answers_ele);
        var data={
            question_pid:index,
            question_no:0,
            question:question,
            answer:answers,
        }; 
        return data;
    }

    function  inputBlur(t){
        var value=$(t).val();
        if(value!=""){
            $(t).parent().parent().attr('data-status','do');
        }
        verify();
    }

    function redo(){
        var value=$("#questionnaire_id").val();
        $("#question_list .question").remove();
        $(".result-list").remove();
        init();
        verify();
        select_click();
        $("#result").hide();
        $("#result footer").before("");
        $("#result").siblings("div,footer").show();
        if(value==2||value==4){
            resultHtml="";
            scoreArray = [];
            sum = 0;
        };
        $(window).scrollTop(0);

    }



